package com.example.bigdata.scala.程序控制结构

object TestDome {
  def main(args: Array[String]): Unit = {
    ifTest
    ifelseTest
    ifelseifTest
    if嵌套
    if表达式
    while语句
    forTest
  }

  def ifTest: Unit ={
    val x = 8
    if (x<10) println(s"$x is smaller than 10")
  }
  def ifelseTest: Unit ={
    val x = 9
    if (x<8) println("small") else println(s"$x big")
  }

  def ifelseifTest: Unit ={
    val x = 8
    if (x==8)
      println("8")
    else  if (x ==7)
      println("7")
    else  if (x == 9)
      println("9")
    else
      println("other")
  }
  def if嵌套: Unit ={
    val x = 10
    if (x<10)
      if(x==9)
        println("9")
      else
        println("other")
    else
      println("bigger than 9")
  }

  def  if表达式: Unit = {
    val x = if("hello"=="hell") 1 else 0
    //println("x : " + x)
    val x1 = if ("hello"=="hell") println("1") else println("0")
  }

  def while语句: Unit ={
    var i = 15
    while(i<20){
      println("i=" + i )
      i+=1
    }
  }

  def forTest {

    // 1 to 5 生成集合包含元素 5
    for( i<- 1 to 5){
      println("i="+i)
    }
    println(1 to 5)

    // until  不好喊 元素 5
    for( i<- 1 until 5){
      println("i="+i)
    }
    // 设置步长 2
    for (i<-1 until(10,2)){
      println("i="+i)
    }

    //  有过滤条件的for 循环
    for (i<-1 to 5 if(i<3)){
      println("i="+i)
    }

    for (i<-1 to 40 if(i%4==0);if(i%5==0)){
      println("i="+i)
    }

    //多重for循环
    for(i<-1 to 5 if (i>3)){
      for (j<- 5 to 7 if(j==6)){
        println("i="+i+",j="+j)
      }
    }

  }
}
